// JavaScript Document
document.addEventListener("DOMContentLoaded", () => {
  const burger = document.querySelector(".burger");
  const nav = document.querySelector(".nav-links");
  const navLinks = document.querySelectorAll(".nav-links li");

  burger.addEventListener("click", () => {
    // Toggle nav
    nav.classList.toggle("active");

    // Animate links
    navLinks.forEach((link, index) => {
      if (link.style.animation) {
        link.style.animation = "";
      } else {
        link.style.animation = `navLinkFade 0.5s ease forwards ${
          index / 7 + 0.3
        }s`;
      }
    });

    // Burger animation
    burger.classList.toggle("active");
  });
});

// 处理菜单项点击
const navLinks = document.querySelectorAll(".nav-links a");

navLinks.forEach((link) => {
  link.addEventListener("click", function (e) {
    // 移除所有链接的active类
    navLinks.forEach((l) => l.classList.remove("active"));
    // 为被点击的链接添加active类
    this.classList.add("active");

    // 如果是移动端,点击后关闭菜单
    if (window.innerWidth <= 768) {
      document.querySelector(".nav-links").classList.remove("active");
      document.querySelector(".burger").classList.remove("active");
    }
  });
});

// 根据当前页面URL设置active类
function setActiveLink() {
  const currentPath = window.location.pathname;
  navLinks.forEach((link) => {
    if (link.getAttribute("href") === currentPath) {
      link.classList.add("active");
    }
  });
}

// 页面加载时设置active链接
window.addEventListener("load", setActiveLink);
